<?php
namespace App\Controller;
use Cake\Collection\Collection;
use Cake\Cache\Cache;

class ServicesController extends AppController{

    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.21
   * ps：获取服务分类
   * */
    public function getCategories()
    {
        $this->loadComponent('SysconfigCache');
        $cid = $this->SysconfigCache->getVal('service_category');
        $this->loadModel('Categories');
        $data = $this->Categories->find('children',['for'=>$cid])
            ->find('treeList',['keyPath'=>'id','valuePath'=>'alias'])
            ->toArray();
        $this->G->success('Success', $data);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.01
   * ps：新增服务项
   * */
    public function add()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $title = $this->request->getData("title");
        $img = $this->request->getData("img");
        $orderNum = $this->request->getData("orderNum");
        $display = $this->request->getData("display");
        $enabled = $this->request->getData("enabled");
        $category_id = $this->request->getData("category_id");
        $target = $this->request->getData("target");
        $url = $this->request->getData("url");
        if(empty($title)){
            $this->G->error("title_error");
            return null;
        }
        $service = $this->Services->newEntity();
        $service->title = $title;
        $service->target = empty($target) ? '':$target;
        $service->url = empty($url) ? '':$url;
        $service->create_user = $this->Jwt->get('user_id');
        $service->order_num = empty($orderNum) ? 0:$orderNum;
        $service->display = empty($display)? 1:$display;
        $service->enabled = empty($enabled)? 1:$enabled;
        $service->img = empty($img) ? '':$this->G->setImgUrl($img);
        $service_save = $this->Services->save($service);
        if(!empty($category_id)){
            $this->loadComponent('CategoryContentCache');
            $this->CategoryContentCache->add('services', $service_save->id, $category_id);
        }
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：修改服务项
   * */
    public function update()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $title = $this->request->getData("title");
        $img = $this->request->getData("img");
        $orderNum = $this->request->getData("orderNum");
        $display = $this->request->getData("display");
        $enabled = $this->request->getData("enabled");
        $category_id = $this->request->getData("category_id");
        $target = $this->request->getData("target");
        $url = $this->request->getData("url");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(empty($title)){
            $this->G->error("title_error");
            return null;
        }
        $service = $this->Services->newEntity();
        $service->id = $id;
        $service->title = $title;
        $service->target = empty($target) ? '':$target;
        $service->url = empty($url) ? '':$url;
        $service->update_user = $this->Jwt->get('user_id');
        $service->order_num = empty($orderNum) ? 0:$orderNum;
        $service->display = empty($display)? 1:$display;
        $service->enabled = empty($enabled)? 1:$enabled;
        $service->img = empty($img) ? '':$this->G->setImgUrl($img);
        $this->Services->save($service);
        $this->loadComponent('CategoryContentCache');
        $this->loadModel('CategoryContents');
        $category_contents = $this->CategoryContents->find()
            ->select(['id','category_id'])
            ->where(['content_type'=>'services','content_id'=>$id])
            ->first();
        if(!empty($category_id)){
            if($category_contents['category_id'] != $category_id){
                $this->CategoryContentCache->add('services', $id, $category_id);
                $this->CategoryContents->deleteAll(['id'=>$category_contents['id']]);
            }
        }else{
            $this->CategoryContents->deleteAll(['id'=>$category_contents['id']]);
        }
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：删除服务项
   * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->Services->query()
            ->update()
            ->set(['is_delete'=>1])
            ->where(['id' => $id])
            ->execute();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：获取服务项列表
   * */
    public function getList()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $title  = $this->request->getData('title');
        $category_id  = $this->request->getData('category_id');
        $where = [];
        $where['is_delete'] = 0;
        if(!empty($title)){
            $where['title like '] = '%'.$title.'%';
        }
        $this->loadModel('CategoryContents');
        if(!empty($category_id)){
            $category_contents = $this->CategoryContents->find()
                ->select(['content_id'])
                ->where(['content_type'=>'services','category_id'=>$category_id])
                ->toArray();
            if(!empty($category_contents)){
                $where['id in'] = array_column($category_contents, 'content_id');
            }
        }
            $services = $this->Services->find()
            ->where($where)
            ->order(['order_num desc','create_time desc'])
            ->toArray();
        $re = [];
        $re['list'] = [];
        if(!empty($services)){
            $this->loadComponent('CategoryContentCache');
            $this->loadModel('Categories');
            foreach ($services as $k=>$v){
                $category_id = $this->CategoryContentCache->getCategories('services', $v['id'])[0];
                $category_name = $this->Categories->findById($category_id)->select(['alias'])->first();
                $re['list'][] = [
                    'id'=>$v['id'],
                    'title'=>$v['title'],
                    'serviceCategoryTitle'=>empty($category_name['alias']) ? '':$category_name['alias'],
                    'serviceCategoryId'=>$category_id,
                    'enabled'=>$v['enabled'],
                    'display'=>$v['display'],
                    'order_num'=>$v['order_num'],
                    'img'=>$v['img'],
                    'url'=>$v['url'],
                    'target'=>$v['target'],
                    'enabledTitle'=>$v['enabled']==1 ? '可用':'不可用',
                ];
            }
        }
        $this->G->success('Success', $re);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：获取指定分类
   * */
    public function getCategory()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $result = $this->CategoryCache->getVal($id);
        $this->G->success('Success', $result);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：分类移动
   * */
    public function move()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $action = $this->request->getData("action");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(!in_array($action, ['up','down'])){
            $this->G->error("action_error");
            return null;
        }
        $result = $this->CategoryCache->move($id, $action);
        if(is_string($result)){
            $this->G->error($result);
            return null;
        }
        $this->G->success('Success', $result);
    }

}
?>